<?php

class Core_Model_Foundation_Collection_Industry extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('foundation/industry');
        $this->from('{t}foundation_industry', 'a')->field('a.*');
    }

    public function joinRelation() {
        return $this->innerJoin('{t}foundation_industry_relation', 'b', 'b.industry_code', 'a.code')
                    ->fields('b.industry_code', 'b.parent_code', 'b.code_path');
    }

    public function sortBy($name, $dir) {
        $dir == 'asc' ? $this->asc("a.$name") : $this->desc("a.$name");
        return parent::sortBy($name, $dir);
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'name':
                return $this->like('a.name', ':name:s')->addParameter('name', "%$arg%");
            case 'ids':
                return $this->contain('a.id', ':ids:i')->addParameter('ids', $arg);
            case 'codes':
                return $this->contain('a.code', ':codes:i')->addParameter('codes', $arg);
        }
        return parent::filterBy($name, $arg);
    }
}